package com.wantupai.mvp.framework.lce.impl.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wantupai.mvp.framework.lce.MvpLceView;
import com.wantupai.mvp.mvp.MvpPresenter;


public abstract class BaseMvpLceFragment<D, V extends MvpLceView<D>, P extends MvpPresenter<V>>
        extends MvpLceFragment<D, V, P> {
    //我们自己的Fragment需要缓存视图
    private View viewContent;//缓存视图
    private boolean isInit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (viewContent == null) {
            viewContent = inflater.inflate(getContentView(), container, false);
            initContentView(viewContent);
            initNavigation((ViewGroup) viewContent);
        }
        //判断Fragment对应的Activity是否存在这个视图
        ViewGroup parent = (ViewGroup) viewContent.getParent();
        if (parent != null) {
            //如果存在,那么我就干掉,重写添加,这样的方式我们就可以缓存视图
            parent.removeView(viewContent);
        }
        return viewContent;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isInit) {
            //只有当Fragment第一次创建的时候，我们去加载数据
            this.isInit = true;
            initData();
        }
    }

    public abstract int getContentView();

    public void initData() {
        loadData(false);
    }

    public void initContentView(View contentView) {

    }

    public void initNavigation(ViewGroup contentView) {

    }

}
